package com.npjscg.medicalpro.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.controller.dto.BusinessDTO;
import com.npjscg.medicalpro.entity.Business;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author raote
 * @since 2022-07-09
 */
public interface BusinessMapper extends BaseMapper<Business> {

    @Select("SELECT * FROM (SELECT bus.uid as uid , bus.id as bid, bus.title as title FROM t_business AS bus LEFT JOIN t_bcheck AS ch ON bus.id = ch.bid WHERE ch.status = #{status}) AS com LEFT JOIN sys_user AS user ON com.uid = user.id" )
    Page<BusinessDTO> findAll(@Param("page") Page<BusinessDTO> page, int status);


    @Insert("insert into t_business(uid,title,contents) values (#{uid},#{title},#{contents})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void saveBusiness(Business business);

    @Select("SELECT * FROM t_business LEFT JOIN t_bcheck ON t_business.id = t_bcheck.bid WHERE t_business.uid = #{id} and t_bcheck.status = #{status} ")
    Page<Business> findAllForU(@Param("page") Page<Business> page, int id, int status);
}
